package com.tjetc.view.teacherview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Student;
import com.zhiyun.domain.Teacher;
import com.zhiyun.domain.User;
import com.zhiyun.service.TeacherService;
import com.zhiyun.service.UserService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.Scanner;

public class MyInfView_Update {
    TeacherService teacherService=CilentProxy.getProxyInstance(TeacherService.class);
    UserService userService= CilentProxy.getProxyInstance(UserService.class);
     Scanner scanner=new Scanner(System.in);

    public void start() {
        while (true){
            System.out.println("~~~~~~~~~~修改信息界面~~~~~~~~~~");
            System.out.println("1.修改我的个人信息");
            System.out.println("2.修改密码");
            System.out.println("3.查看我的个人信息");
            System.out.println("0.返回上一级");
            System.out.print("请输入选择:");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    updateMyInf();
                    break;
                case 2:
                    updateMyPassWord();
                    break;
                case 3:
                    findMyInf();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }

        }
    }

    private void findMyInf() {
       while (true){
           Result result = teacherService.myInfo(AppData.teacher.getId());
           if (result.getCode()==200) {
               Teacher obj = (Teacher) result.getObj();
               System.out.println("昵称:" + obj.getNickname());
               System.out.println("姓名:" + obj.getTruename());
               System.out.println("手机号:" + obj.getPhone());
               System.out.println("邮箱:" + obj.getEmail());
               System.out.println("性别:" + obj.getSex());
               System.out.println("生日:" + obj.getBirthday());
           }
           break;
       }

    }

    private void updateMyPassWord() {
        while (true){
            System.out.println("请输入原密码");
            String password = scanner.next();
            if (password.equals(AppData.loginUser.getPassword())){
                System.out.println("请输入修改后的密码:");
                String newpassword = scanner.next();
                Result result = userService.UpdatePassword
                        (AppData.loginUser.getId(),newpassword);
                int code = result.getCode();
                if (code==200){
                    AppData.loginUser.setPassword(newpassword);
                    System.out.println("修改成功");
                }else {
                    System.out.println("修改失败");
                }
                break;
            }else {
                System.out.println("原密码输入错误");
            }

        }
    }

    private void updateMyInf() {
        while (true){
            Result result = teacherService.myInfo(AppData.teacher.getId());
            if (result.getCode()==200){
                Teacher obj = (Teacher) result.getObj();
                System.out.println("昵称:"+obj.getNickname());
                System.out.println("姓名:"+obj.getTruename());
                System.out.println("手机号:"+obj.getPhone());
                System.out.println("邮箱:"+obj.getEmail());
                System.out.println("性别:"+obj.getSex());
                System.out.println("生日:"+obj.getBirthday());
                while (true){
                    System.out.print("请输入修改后的昵称:");
                    String nickname = scanner.next();
                    System.out.print("请输入修改后的姓名:");
                    String truename = scanner.next();
                    System.out.print("请输入修改后的手机号:");
                    String phone = scanner.next();
                    System.out.print("请输入修改后的邮箱:");
                    String email = scanner.next();
                    System.out.print("请输入修改后的性别:");
                    String sex = scanner.next();
                    System.out.print("请输入修改后的生日:");
                    String birthday = scanner.next();
                    Teacher teacher = AppData.teacher;
                    Teacher teacher1 = new Teacher(teacher.getId(), teacher.getUserId(),
                            nickname, truename, phone, email, sex, birthday, teacher.getCompany()
                            , teacher.getCid(), teacher.getUser(), teacher.getCourses());
                    Result result1 = teacherService.updateTeacher(teacher1);
                    if (result1.getCode()==200){
                        Teacher obj1 = (Teacher) result1.getObj();
                        AppData.teacher.setNickname(nickname);
                        AppData.teacher.setTruename(truename);
                        AppData.teacher.setPhone(phone);
                        AppData.teacher.setEmail(email);
                        AppData.teacher.setSex(sex);
                        AppData.teacher.setBirthday(birthday);
                        System.out.println("修改成功~~~~~~~~~~~");
                    }else {
                        System.out.println("修改失败");
                    }
                    break;
                }



            }else {
                System.out.println("身份信息不存在,不能修改~~~");
            }
            break;
        }
    }
}
